﻿using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using EnsureWeb.Web.DataModel;
using System.Reflection;
using System;
using System.ComponentModel;

namespace EnsureWeb.UI.ViewModels
{
    public partial class MainViewModel : ViewModelBase
    {
        private bool isShowAdminView;
        public bool IsShowAdminView
        {
            get
            {
                return isShowAdminView;
            }
            set
            {
                isShowAdminView = value;
                RaisePropertyChanged("IsShowAdminView");
            }
        }

        private ICommand showAdminView;
        public ICommand ShowAdminView
        {
            get
            {
                if (showAdminView == null)
                    showAdminView = new RelayCommand(() =>
                    {
                        this.IsShowAdminView = true;
                    });

                return showAdminView;
            }
        }

        public ICommand editItem;
        public ICommand EditItem
        {
            get
            {
                if (editItem == null)
                    editItem = new RelayCommand(() =>
                    {
                        if (this.DomainContext.HasChanges && !this.DomainContext.IsSubmitting)
                        {
                            this.DomainContext.SubmitChanges();
                        }
                    });

                return editItem;
            }
        }

        public ICommand deletingItem;
        public ICommand DeletingItem
        {
            get
            {
                if (deletingItem == null)
                    deletingItem = new RelayCommand<CancelEventArgs>(e =>
                    {   
                        if (MessageBox.Show("你确定要删除吗？", "删除", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                            e.Cancel = true;
                    });

                return deletingItem;
            }
        }

        public ICommand deleleItem;
        public ICommand DeleleItem
        {
            get
            {
                if (deleleItem == null)
                    deleleItem = new RelayCommand(() =>
                    {
                        if (this.DomainContext.HasChanges && !this.DomainContext.IsSubmitting)
                        {
                            this.DomainContext.SubmitChanges();
                        }
                    });

                return deleleItem;
            }
        }

        public ICommand adminContainerLoaded;
        public ICommand AdminContainerLoaded
        {
            get
            {
                if (adminContainerLoaded == null)
                    adminContainerLoaded = new RelayCommand<Panel>(e =>
                    {
                        this.adminContainer = e;

                        this.ShowView("AdminProject");
                    });

                return adminContainerLoaded;
            }
        }

        public ICommand adminMenuItemClick;
        public ICommand AdminMenuItemClick
        {
            get
            {
                if (adminMenuItemClick == null)
                    adminMenuItemClick = new RelayCommand<string>(viewName =>
                    {
                        this.ShowView(viewName);
                    });

                return adminMenuItemClick;
            }
        }

        private void ShowView(string viewName)
        {
            if (string.IsNullOrEmpty(viewName))
                return;


            if (viewName == "None")
            {
                EnsureWeb.Controls.Window.Alert("This function hasn't implemented");
                return;
            }

            UserControl view = this.adminContainer.FindName(string.Format("AdminContainer_{0}", viewName)) as UserControl;
            if (view == null)
            {
                view = this.CreateView(viewName);
                view.Name = string.Format("AdminContainer_{0}", viewName);
                this.adminContainer.Children.Add(view);
            }
            else
            {
                this.adminContainer.Children.Remove(view);
                this.adminContainer.Children.Add(view);
            }
        }

        private UserControl CreateView(string viewName)
        {
            if (viewName != string.Empty)
            {
                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                foreach (Type type in executingAssembly.GetTypes())
                {
                    if (type.Name.EndsWith(viewName))
                    {
                        UserControl panel = Activator.CreateInstance(type) as UserControl;
                        return panel;
                    }
                }
            }

            return null;
        }

        private void InitAdminView()
        {
        }

        private Panel adminContainer;
    }

}